﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlClient;

namespace IHISCETeim.DAL
{
    public class VIPCustomerDAL
    {
        public static int Add(VIPCustomerInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPCustomer_Add");
            DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
            DB.AddInParameter(dbCommand, "IDCard", DbType.String, obj.IDCard);
            DB.AddInParameter(dbCommand, "Tel", DbType.String, obj.Tel);
            DB.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
            DB.AddInParameter(dbCommand, "IsVIP", DbType.Boolean, obj.IsVIP);
            DB.AddInParameter(dbCommand, "VIPCardNum", DbType.String, obj.VIPCardNum);
            DB.AddInParameter(dbCommand, "VIPCardType", DbType.String, obj.VIPCardType);
            DB.AddInParameter(dbCommand, "ValidFrom", DbType.DateTime, obj.ValidFrom);
            DB.AddInParameter(dbCommand, "ValidTo", DbType.DateTime, obj.ValidTo);
            DB.AddInParameter(dbCommand, "TotalPoint", DbType.Int32, obj.TotalPoint);
            DB.AddInParameter(dbCommand, "AvailablePoint", DbType.Int32, obj.AvailablePoint);

            SqlParameter _LastBuyDate = new SqlParameter("@LastBuyDate", SqlDbType.DateTime);
            _LastBuyDate.IsNullable = true;
            if (obj.LastBuyDate == DateTime.MinValue)
                _LastBuyDate.Value = DBNull.Value;
            else
                _LastBuyDate.Value = obj.LastBuyDate;
            dbCommand.Parameters.Add(_LastBuyDate);

            SqlParameter _LastExchangeDate = new SqlParameter("@LastExchangeDate", SqlDbType.DateTime);
            _LastExchangeDate.IsNullable = true;
            if (obj.LastExchangeDate == DateTime.MinValue)
                _LastExchangeDate.Value = DBNull.Value;
            else
                _LastExchangeDate.Value = obj.LastExchangeDate;
            dbCommand.Parameters.Add(_LastExchangeDate);




            DB.AddInParameter(dbCommand, "UserId", DbType.Int32, obj.UserId);
            DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
            DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int CustomerId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPCustomer_Delete", CustomerId);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetList(int CustomerId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPCustomer_GetList", CustomerId);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPCustomer_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        public static int Update(VIPCustomerInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPCustomer_Update");
            DB.AddInParameter(dbCommand, "CustomerId", DbType.Int32, obj.CustomerId);
            DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
            DB.AddInParameter(dbCommand, "IDCard", DbType.String, obj.IDCard);
            DB.AddInParameter(dbCommand, "Tel", DbType.String, obj.Tel);
            DB.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
            DB.AddInParameter(dbCommand, "IsVIP", DbType.Boolean, obj.IsVIP);
            DB.AddInParameter(dbCommand, "VIPCardNum", DbType.String, obj.VIPCardNum);
            DB.AddInParameter(dbCommand, "VIPCardType", DbType.String, obj.VIPCardType);
            DB.AddInParameter(dbCommand, "ValidFrom", DbType.DateTime, obj.ValidFrom);
            DB.AddInParameter(dbCommand, "ValidTo", DbType.DateTime, obj.ValidTo);
            DB.AddInParameter(dbCommand, "UserId", DbType.Int32, obj.UserId);
            DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
            DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

            try
            {
                return  Convert.ToInt32(   DB.ExecuteScalar(dbCommand));
               // return 1;
            }
            catch (Exception exc)
            {
                return 0;
            }
        }
    }
}
